#include "graphics.h"
#include <stdio.h>

typedef struct {
	double x, y;
} pointT;

void drawTriangle(pointT a, pointT b, pointT c, int counter);
pointT calculate(pointT p1, pointT p2);

int main() {
	int counter = 0;
	pointT a = {3.5, 3.5}, b = {1.0, 0.5}, c = {6.0, 0.5};
	InitGraphics();
	drawTriangle(a, b, c, counter);
	return 0;
}

void drawTriangle(pointT a, pointT b, pointT c, int counter) {
	if (counter >= 5) return;
	MovePen(a.x, a.y);
	DrawLine(b.x-a.x, b.y-a.y);
	DrawLine(c.x-b.x, c.y-b.y);
	DrawLine(a.x-c.x, a.y-c.y);

	drawTriangle(a, calculate(b, a), calculate(c, a), counter+1);
	drawTriangle(calculate(a, b), b, calculate(c, b), counter+1);
	drawTriangle(calculate(a, c), calculate(b, c), c, counter+1);
	drawTriangle(calculate(b, c), calculate(b, a), calculate(c, a), counter+1);
}

pointT calculate(pointT p1, pointT p2){
	pointT p;
	p.x = (p1.x+p2.x)/2;
	p.y = (p1.y+p2.y)/2;
	return p;
}